//创建用户相关的小仓库
import { defineStore } from 'pinia';
//引入请求函数
import { reqLogin, reqLogout } from "@/api/userinfo/login";
import { reqUpdate, reqChangepass, reqUsers, reqDeleteUsers, reqAddUsers } from '@/api/userinfo/userDialog';
//引入持久化存储token方法
import { setToken, getToken, removeToken } from "@/utils/token-utils";
// 引入组件库
import { ElMessage } from 'element-plus'
import { staticRoutes } from '@/router/routes'
//引入路由器对象
import router from '@/router';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfoStore = defineStore('userInfo', {

  state: () => ({
    theme:'theme1',
    AccessToken: sessionStorage.getItem('AccessToken') ? sessionStorage.getItem('AccessToken') : "",//从本地存储获取token
    TokenType: sessionStorage.getItem('TokenType') ? sessionStorage.getItem('TokenType') : "",
    // Userinfo: sessionStorage.getItem('Userinfo') ? sessionStorage.getItem('Userinfo') : {} ,   //用户信息
    AccountName: "",   //用户名
    userInfo: {},
    usersInfo: [],
    desc: '',
    changePasswordDesc: '',
    statesObj:{},
    deleteInfo:{}
  }),
  //书写小仓库方法的地方
  actions: {
    //登录方法
    //如果登录请求:登录成功，第一次遇见token
    async login(AccountName: string, AccountPwd: string) {
      let data = { AccountName, AccountPwd };
      try {
        let res: any = await reqLogin(data)
        //pinia仓库存储一份
        if (res.Desc) {
          this.desc = res.Desc
        } else {
          this.AccessToken = res.AccessToken;
          this.TokenType = res.TokenType;
          this.AccountName = res.UserInfo.AccountName
          this.userInfo = res
          //本地存储存储一份
          sessionStorage.setItem("AccessToken", res.AccessToken)
          sessionStorage.setItem("TokenType", res.TokenType)
          // sessionStorage.setItem("AccountName", JSON.stringify(res.UserInfo.AccountName))
          // const userinfo = res.UserInfo
          localStorage.setItem('UserInfo', JSON.stringify(res.UserInfo));
        }



      } catch (error) {
        console.log(error);
      }

    },

    //退出登录
    async reset() {
      try {
        //退出登录请求
        await reqLogout();
        //清除pinia仓库数据
        this.AccessToken = '';
        this.TokenType = '';
        //本地存储
        removeToken();
        // sessionStorage.setItem("AccessToken", '')
        // sessionStorage.setItem("TokenType", '')
      } catch (error) {
        console.log(error);
      }
    },

    //更新用户信息
    async updateUserinfo(data: any) {
      try {
        let res = await reqUpdate(data);
        this.statesObj = res
      } catch (error) {
      }
    },

    // 修改账号密码
    async changePass(data: any) {
      try {
        let res = await reqChangepass(data);
        this.changePasswordDesc = res.Desc
      } catch (error) {
      }
    },

    // 获取所有用户信息
    async usersinfo() {
      try {
        let res = await reqUsers();
        console.log("🚀 ~ file: userInfo.ts:104 ~ usersinfo ~ res:", res)
        this.usersInfo = res
      } catch (error) {
        console.log(error);

      }
    },

    // 删除用户
    async deleteUsers(accountName: any) {
      try {
        let res = await reqDeleteUsers(accountName);
        this.deleteInfo = res
      } catch (error) {
        console.log(error);

      }
    },

    // 添加用户
    async addUserinfo(accountName: any) {
      try {
        let res = await reqAddUsers(accountName);
      } catch (error) {
        console.log(error);

      }
    }
  },
});
